<template>
  <!-- 搜索工作栏 -->
  <ContentWrap ref="form">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="软件类型" prop="softwareType">
        <el-select
          v-model="queryParams.softwareType"
          placeholder="请选择软件类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_SOFTWARE_VERSION_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学校" prop="schoolId">
        <el-select
          v-model="queryParams.schoolId"
          placeholder="请选择学校"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option
            v-for="item in schoolList"
            :key="item.id"
            :label="item.schoolName + '-' + item.tenantName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="平台ID" prop="platformId">
        <el-input
          v-model.trim="queryParams.platformId"
          placeholder="请输入平台ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_SOFTWARE_VERSION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="md5值" prop="apkMd5">
        <el-input
          v-model.trim="queryParams.apkMd5"
          placeholder="请输入md5值"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="文件大小" prop="apkSize">
        <el-input
          v-model.trim="queryParams.apkSize"
          placeholder="请输入文件大小"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="下载URL" prop="downloadUrl">
        <el-input
          v-model.trim="queryParams.downloadUrl"
          placeholder="请输入下载URL"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="更新状态" prop="updateStatus">
        <el-select
          v-model="queryParams.updateStatus"
          placeholder="请选择更新状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_SOFTWARE_VERSION_UPDATE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="版本编码" prop="versionCode">
        <el-input
          v-model.trim="queryParams.versionCode"
          placeholder="请输入版本编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="版本名称" prop="versionName">
        <el-input
          v-model.trim="queryParams.versionName"
          placeholder="请输入版本名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:software-version:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <!-- <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:software-version:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap :setPadding="false">
    <el-table v-loading="loading" :data="list" stripe :max-height="tableHeight">
      <!-- <el-table-column label="ID" prop="id" /> -->
      <el-table-column label="软件类型" prop="softwareType"
        ><template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SYSTEM_SOFTWARE_VERSION_TYPE"
            :value="scope.row.softwareType"
          /> </template
      ></el-table-column>
      <el-table-column label="学校" prop="schoolName" show-overflow-tooltip />
      <el-table-column label="平台ID" prop="platformId" show-overflow-tooltip />
      <el-table-column label="状态" prop="status"
        ><template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SYSTEM_SOFTWARE_VERSION_STATUS"
            :value="scope.row.status"
          /> </template
      ></el-table-column>
      <el-table-column label="更新状态" prop="updateStatus"
        ><template #default="scope">
          <dict-tag
            :type="DICT_TYPE.SYSTEM_SOFTWARE_VERSION_UPDATE_STATUS"
            :value="scope.row.updateStatus"
          /> </template
      ></el-table-column>
      <el-table-column label="md5值" prop="apkMd5" show-overflow-tooltip />
      <el-table-column label="文件大小" prop="apkSize" show-overflow-tooltip />
      <el-table-column label="下载URL" prop="downloadUrl" show-overflow-tooltip />
      <el-table-column label="版本编码" prop="versionCode" show-overflow-tooltip />
      <el-table-column label="版本名称" prop="versionName" show-overflow-tooltip />
      <el-table-column label="修改内容" prop="modifyContent" show-overflow-tooltip />
      <el-table-column
        label="创建时间"
        prop="createTime"
        :formatter="dateFormatter"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="uploadForm(scope.row.id)"
            v-hasPermi="['system:software-version:upload']"
          >
            上传
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:software-version:update']"
          >
            编辑
          </el-button>
          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:software-version:delete']"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SoftwareVersionForm ref="formRef" @success="getList" />
  <FileForm ref="uploadRef" @success="getList" />
</template>

<script setup lang="ts" name="SoftwareVersion">
import { dateFormatter } from '@/utils/formatTime'
// import download from '@/utils/download'
import * as SoftwareVersionApi from '@/api/system/softwareVersion'
import SoftwareVersionForm from './SoftwareVersionForm.vue'
import * as BaseSchoolApi from '@/api/cmis/school'
import FileForm from './FileForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
// const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化
const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  schoolId: null,
  platformId: null,
  softwareType: null,
  status: null,
  apkMd5: null,
  apkSize: null,
  downloadUrl: null,
  modifyContent: null,
  updateStatus: null,
  versionCode: null,
  versionName: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
// const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SoftwareVersionApi.getSoftwareVersionPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 上传安装包操作 */
const uploadRef = ref()
const uploadForm = (id: number) => {
  uploadRef.value.open(id)
}

/** 删除按钮操作 */
// const handleDelete = async (id: number) => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     // 发起删除
//     await SoftwareVersionApi.deleteSoftwareVersion(id)
//     message.success(t('common.delSuccess'))
//     // 刷新列表
//     await getList()
//   } catch {}
// }

/** 导出按钮操作 */
// const handleExport = async () => {
//   try {
//     // 导出的二次确认
//     await message.exportConfirm()
//     // 发起导出
//     exportLoading.value = true
//     const data = await SoftwareVersionApi.exportSoftwareVersion(queryParams)
//     download.excel(data, '系统软件版本.xls')
//   } catch {
//   } finally {
//     exportLoading.value = false
//   }
// }

/** 查询学校列表[忽略租户] */
const schoolList = ref<BaseSchoolApi.BaseSchoolVO[]>([])
const getSchoolList = async () => {
  try {
    const data = await BaseSchoolApi.selectAllBaseSchoolList({})
    schoolList.value = data
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
  getSchoolList()
})
</script>
